package com.itheima.reggie.controller;/**
 * 功能描述
 *
 * @author 唯硕
 * @date 2022/09/20  16:38
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 *@ClassName CotegoryController
 *@Description TODO 菜品类别，套餐分类
 *@Author 唯硕
 *@Date 2022/9/20 16:38
 *@Version 1.0
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    //新增分类
    @PostMapping
    public R<String> addCategory(HttpServletRequest request, @RequestBody Category category){
        boolean save = categoryService.save(category);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        category.setCreateUser(empId);
//        category.setUpdateUser(empId);
        if(save)
            return R.success("保存成功");
        return R.error("保存失败");

    }


    //分页查询
    @GetMapping("/page")
    public R<Page>  getPage( int page, int pageSize){
        Page<Category> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(page1,queryWrapper);
        return R.success(page1);

    }

    //删除分类
    @DeleteMapping
    public  R<String> Delete(Long ids){
        categoryService.remove(ids);

        return R.success("分类信息删除成功");
    }

    //修改分类
    @PutMapping
    public R<String> update(Category category){
        categoryService.updateById(category);
        return  R.success("修改分类信息成功");
    }

    //获取菜品、套餐分类
 @GetMapping("/list")
    public R<List<Category>> list(Category category){
     LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
     categoryLambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
     categoryLambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
     List<Category> list = categoryService.list(categoryLambdaQueryWrapper);


     return R.success(list);

 }
}
